#include<bits/stdc++.h>
#define N 1000005
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
int n;
int len[N],o[N];
int ans,mx;
vector<int> g[N];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > p;
signed main(){
    freopen("contest.in","r",stdin);
    freopen("contest.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++){
        len[i]=read();
        for(int j=1,x;j<=len[i];j++){
            x=read();
            g[i].push_back(x);
        }
        sort(g[i].begin(),g[i].end());
    }
    for(int i=1;i<=n;i++) p.push({g[i][0],i}),mx=max(mx,g[i][0]);
    ans=mx-p.top().first;
    int cnt=0;
    while(1){
        int x=p.top().second;
        p.pop();
        o[x]++;
        if(o[x]>=len[x]) break;
        p.push({g[x][o[x]],x});
        mx=max(mx,g[x][o[x]]);
        ans=min(ans,mx-p.top().first);
    }
    printf("%d\n",ans);
    return 0;
}